import urlConfig from "@/common/utils/config";
import ryutil from "@/common/utils/rsa/ryutil";
const request = {};
request.globalRequest = (url, method, data) => {
  let setData = "";
  if (method == "POST") {
    setData = JSON.stringify(data);
    return uni
      .request({
        url: urlConfig.base_url + url,
        method,
        data: setData,
        dataType: "json",
        header: {
          "Authorization": "Bearer " + uni.getStorageSync("token"),
          "content-type": "application/json",
          "Content-Security-Policy":
            "script-src 'self'; object-src 'none';style-src cdn.example.org third-party.org; child-src https:",
        },
      })
      .then((res) => {
        console.log('post>>>>>>>>>>>',res)
        let data = res[1].data;
        if (data.success) {
          return data;
        } else {
          if (
            data.code == "10401"
          ) {
            setTimeout(() => {
              uni.reLaunch({ url: "/pages/login/index" });
            }, 200);
          } else {
            uni.showToast({
              title: data.msg,
              icon: "none",
            });
            return data;
          }
        }
      })
      .catch((parmas) => {
        if (parmas) {
          switch (parmas.code) {
            case 401:
              uni.clearStorageSync();
              break;
            default:
              uni.showToast({
                title: "网络不给力，请稍后再试",
                icon: "none",
              });
              return Promise.reject();
          }
        }
      });
  }else {
    return uni
      .request({
        url: urlConfig.base_url + url,
        method,
        data: data,
        dataType: "json",
        header: {
          "Authorization": "Bearer " + uni.getStorageSync("token"),
          "content-type": "application/json",
          "Content-Security-Policy":
            "script-src 'self'; object-src 'none';style-src cdn.example.org third-party.org; child-src https:",
        },
      })
      .then((res) => {
        console.log('get>>>>>>>>>>>',res)
        let data = res[1].data;
        console.log(data)
        if (data.success) {
          return data;
        } else {
          if (
            data.code == "10401"
          ) {
            uni.showToast({
              title: data.msg,
              icon: "none",
            });
            //未登录//ID不能为空//找不到RSA秘钥
            setTimeout(() => {
              uni.reLaunch({ url: "/pages/login/index" });
            }, 200);
          } else {
            uni.showToast({
              title: data.msg,
              icon: "none",
            });
            return data;
          }
        }
      })
      .catch((parmas) => {
        console.log('1111111')
        console.log(parmas)
        if (parmas) {
          switch (parmas.code) {
            case 401:
              uni.clearStorageSync();
              break;
            default:
              uni.showToast({
                title: "网络不给力，请稍后再试",
                icon: "none",
              });
              return Promise.reject();
          }
        }
      });
  }
};
export default request;
